#include <sstream>
#include <string>
#include <ctime>
#include <cstdlib>

#include "UUID.h"

using namespace std;

UUID::UUID(): seeds("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") {}

string UUID::timestamp() {
	stringstream ss;
	
	time_t current = time(0);
	tm ctm;
	localtime_s(&ctm, &current);
	ss<<ctm.tm_year + 1900;
	if(ctm.tm_mon < 9) {
		ss<<0<<ctm.tm_mon + 1;
	} else {
		ss<<ctm.tm_mon;
	}
	if(ctm.tm_mday < 10) {
		ss<<0<<ctm.tm_mday;
	} else {
		ss<<ctm.tm_mday;
	}
	if(ctm.tm_hour < 10) {
		ss<<0<<ctm.tm_hour;
	} else {
		ss<<ctm.tm_hour;
	}
	if(ctm.tm_min < 10) {
		ss<<0<<ctm.tm_min;
	} else {
		ss<<ctm.tm_min;
	}
	if(ctm.tm_sec < 10) {
		ss<<0<<ctm.tm_sec;
	} else {
		ss<<ctm.tm_sec;
	}
	
	return ss.str();
}

unsigned UUID::random(unsigned top) {
	return 1 + (int) (top * (rand() / (RAND_MAX + 1.0)));
}

string UUID::uuid(unsigned count) {
	string result;
	for(int i = 0; i < count; i++) {
		result.append(1, seeds[random(36) - 1]);
	}
	return result;
}
